home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / artdisplay / LocalCoverArtSearch.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.9 KB  |  181 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import rhythmdb
  6. import gnomevfs
  7. import rb
  8. import gobject
  9. IMAGE_NAMES = [
  10.     'cover',
  11.     'album',
  12.     'albumart',
  13.     '.folder',
  14.     'folder']
  15. LOAD_DIRECTORY_FLAGS = gnomevfs.FILE_INFO_GET_MIME_TYPE | gnomevfs.FILE_INFO_FORCE_FAST_MIME_TYPE | gnomevfs.FILE_INFO_FOLLOW_LINKS
  16. ITEMS_PER_NOTIFICATION = 10
  17. ART_SAVE_NAME = 'Cover.jpg'
  18. ART_SAVE_FORMAT = 'jpeg'
  19. ART_SAVE_SETTINGS = {
  20.     'quality': '100' }
  21.  
  22. def file_root(f_name):
  23.     return os.path.splitext(f_name)[0].lower()
  24.  
  25.  
  26. def shared_prefix_length(a, b):
  27.     l = 0
  28.     while a[l] == b[l]:
  29.         l = l + 1
  30.     return l
  31.  
  32.  
  33. class LocalCoverArtSearch:
  34.     
  35.     def __init__(self):
  36.         pass
  37.  
  38.     
  39.     def _load_dir_cb(self, handle, files, exception, .4):
  40.         (results, on_search_completed_cb, entry, args) = .4
  41.         for f in files:
  42.             if f.mime_type is None:
  43.                 continue
  44.             if f.mime_type.startswith('image/') and f.permissions & gnomevfs.PERM_USER_READ:
  45.                 results.append(f.name)
  46.                 continue
  47.         
  48.         if exception:
  49.             on_search_completed_cb(self, entry, results, *args)
  50.             if not issubclass(exception, gnomevfs.EOFError):
  51.                 print 'Error reading "%s": %s' % (self.uri.parent, exception)
  52.             
  53.         
  54.  
  55.     
  56.     def search(self, db, entry, on_search_completed_cb, *args):
  57.         self.uri = None
  58.         
  59.         try:
  60.             self.uri = gnomevfs.URI(entry.get_playback_uri())
  61.         except TypeError:
  62.             pass
  63.  
  64.         if self.uri is None or self.uri.scheme == 'http':
  65.             print 'not searching for local art for %s' % self.uri
  66.             on_search_completed_cb(self, entry, [], *args)
  67.             return None
  68.         self.artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
  69.         self.album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
  70.         print 'searching for local art for %s' % self.uri
  71.         gnomevfs.async.load_directory(self.uri.parent, self._load_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data = ([], on_search_completed_cb, entry, args))
  72.  
  73.     
  74.     def search_next(self):
  75.         return False
  76.  
  77.     
  78.     def get_best_match_urls(self, results):
  79.         for name in [
  80.             file_root(self.uri.short_name)] + IMAGE_NAMES:
  81.             for f_name in results:
  82.                 if file_root(f_name) == name:
  83.                     yield self.uri.parent.append_file_name(f_name)
  84.                     continue
  85.             
  86.         
  87.         artist = self.artist.lower()
  88.         album = self.album.lower()
  89.         for f_name in results:
  90.             f_root = file_root(f_name).lower()
  91.             if f_root.find(artist) != -1 and f_root.find(album) != -1:
  92.                 yield self.uri.parent.append_file_name(f_name).path
  93.                 continue
  94.         
  95.         match = (2, None)
  96.         for f_name in results:
  97.             pl = shared_prefix_length(f_name, self.uri.short_name)
  98.             if pl > match[0]:
  99.                 match = (pl, f_name)
  100.                 continue
  101.         
  102.         if match[1] is not None:
  103.             yield self.uri.parent.append_file_name(match[1]).path
  104.         
  105.  
  106.     
  107.     def pixbuf_save(self, plexer, pixbuf, uri):
  108.         gnomevfs.async.create(uri, plexer.send(), gnomevfs.OPEN_WRITE | gnomevfs.OPEN_TRUNCATE, False, 420, gnomevfs.PRIORITY_DEFAULT)
  109.         yield None
  110.         (handle, result) = (_,)
  111.         if result:
  112.             print 'Error creating "%s": %s' % (uri, result)
  113.             return None
  114.         
  115.         def pixbuf_cb(buf):
  116.             data = [
  117.                 buf]
  118.             status = []
  119.             
  120.             def write_coro(w_plexer):
  121.                 while data:
  122.                     buf = data.pop()
  123.                     handle.write(buf, w_plexer.send())
  124.                     yield None
  125.                     (_, requested, result, written) = (_,)
  126.                     if result:
  127.                         print 'Error writing "%s": %s' % (uri, result)
  128.                         status.insert(0, False)
  129.                         return None
  130.                     if written < requested:
  131.                         data.insert(0, buf[written:])
  132.                         continue
  133.                     result
  134.                     continue
  135.                     w_plexer.receive()
  136.                 status.insert(0, True)
  137.  
  138.             rb.Coroutine(write_coro).begin()
  139.             while not status:
  140.                 gobject.main_context_default().iteration()
  141.                 continue
  142.                 (None, None, None, None)
  143.             return status[0]
  144.  
  145.         pixbuf.save_to_callback(pixbuf_cb, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
  146.         handle.close(plexer.send())
  147.         yield None
  148.         (plexer.receive(), result)
  149.         (_, result) = (_,)
  150.  
  151.     
  152.     def _save_dir_cb(self, handle, files, exception, .4):
  153.         (db, entry, pixbuf) = .4
  154.         (artist, album) = [ db.entry_get(entry, x) for x in [
  155.             rhythmdb.PROP_ARTIST,
  156.             rhythmdb.PROP_ALBUM] ]
  157.         for f in files:
  158.             uri = str(self.uri.parent.append_file_name(f.name))
  159.             u_entry = db.entry_lookup_by_location(uri)
  160.             print 'Not saving local art; encountered unknown file (%s)' % uri
  161.             handle.cancel()
  162.             return None
  163.         
  164.  
  165.     
  166.     def save_pixbuf(self, db, entry, pixbuf):
  167.         self.uri = None
  168.         
  169.         try:
  170.             self.uri = gnomevfs.URI(entry.get_playback_uri())
  171.         except TypeError:
  172.             pass
  173.  
  174.         if self.uri is None or self.uri.scheme == 'http':
  175.             print 'not saving local art for %s' % self.uri
  176.             return None
  177.         print 'checking whether to save local art for %s' % self.uri
  178.         gnomevfs.async.load_directory(self.uri.parent, self._save_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data = (db, entry, pixbuf))
  179.  
  180.  
  181.